<?php

declare(strict_types=1);

namespace app\admin\service;

use app\admin\common\ServiceResponse;
use app\admin\model\MinerFirmwareversion as MinerFirmwareversionModel;
use app\admin\model\Images;

class MinerFirmwareversion  extends BaseService
{
    /**
     * 注册服务
     *
     * @return mixed
     */
    public function register()
    {
        //
    }

    /**
     * 执行服务
     *
     * @return mixed
     */
    public function boot()
    {
        //
    }

    use ServiceResponse;
    protected $model;
    public function __construct()
    {
        $this->model = new MinerFirmwareversionModel();
    }


    /**
     * @describe
     * @author: Bobo
     * @date: 2025-06-27 18:06:25 
     * @param [type] @required
     * @return Renderable
     */
    public function create($params)
    {

        $pids = explode(',', $params['pid']);
        $data = [];
        foreach ($pids as $pid) {
            $data[] = [
                'pid' => $pid,
                'firmware_fileid' => $params['firmware_fileid'],
                'version_id' => $params['version_id'],
                'release_date' => $params['release_date'],
                'create_time' => date('Y-m-d H:i:s'),
                'creator' => $params['creator'],
            ];
        }

        $this->model->saveAll($data);
        return $this->success();
    }

    /**
     * @describe
     * @author: Bobo
     * @date: 2025-06-27 18:20:34 
     * @param [type] @required
     * @return Renderable
     */
    public function getList(int $page = 1, int $limit = 10, array $where = [], $field = "*")
    {


        $query = $this->model
            ->with(['pid', 'creator', 'updater'])
            ->field('id,pid,firmware_fileid,version_id,release_date,create_time,creator,updater,update_time')
            ->order('id desc');

        // 处理复杂的 where 条件
        if (!empty($where)) {
            foreach ($where as $key => $condition) {
                if (is_numeric($key) && is_array($condition)) {
                    // 处理数组格式的条件，如: ['pid', 'in', [...]] 或 ['release_date', 'between', [...]]
                    if (count($condition) >= 3) {
                        $query->where($condition[0], $condition[1], $condition[2]);
                    } elseif (count($condition) == 2) {
                        $query->where($condition[0], $condition[1]);
                    }
                } elseif (is_array($condition)) {
                    // 处理关联数组格式的条件，如: ['is_del' => 0]
                    foreach ($condition as $field => $value) {
                        $query->where($field, $value);
                    }
                } else {
                    // 处理简单的键值对条件
                    $query->where($key, $condition);
                }
            }
        }


        $total = $query->count();
        $list = $query->page($page, $limit)->select()->toArray();

        $firmware_fileids = array_column($list, 'firmware_fileid');
        $firmware_fileids = array_unique($firmware_fileids);

        $images = new Images();
        $firmware_fileid_urls = $images->getImagesByIds($firmware_fileids);
        $firmware_fileid_urls = array_column($firmware_fileid_urls, 'url', 'id');
        foreach ($list as $key => $value) {
            $list[$key]['firmware_fileid_url'] = $firmware_fileid_urls[$value['firmware_fileid']] ?? '';
        }

        return $this->success([
            'total' => $total,
            'list' => $list
        ]);
    }

    /**
     * @describe
     * @author: Bobo
     * @date: 2025-06-27 18:51:28 
     * @param [type] @required
     * @return Renderable
     */
    public function getInfo($params)
    {
        $id = $params['id'];
        $data = $this->model->alias('mfv')->leftJoin('product p', 'p.id = mfv.pid')->field('mfv.*,p.cname as pid_name')->with(['creator', 'updater'])->find($id);
        $data = $data->toArray();
        $images = new Images();
        $firmware_fileid_urls = $images->getImagesByIds($data['firmware_fileid']);
        $data['firmware_fileid_list'] = $firmware_fileid_urls;
        $data['firmware_fileid_url'] = $firmware_fileid_urls[0]['url'] ?? '';
        return $this->success($data);
    }

    /**
     * @describe
     * @author: Bobo
     * @date: 2025-06-27 19:13:57 
     * @param [type] @required
     * @return Renderable
     */
    public function update($params)
    {
        $id = $params['id'];
        $data = $this->model->find($id);
        if (!$data) {
            return $this->error(400000);
        }
        $data->save($params);
        return $this->success();
    }

    /**
     * @describe
     * @author: Bobo
     * @date: 2025-06-27 19:18:34 
     * @param [type] @required
     * @return Renderable
     */
    public function delete($id)
    {
        $this->model->where('id', $id)->update(['is_del' => 1]);
        return $this->success();
    }
}
